home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 016a / faketo4d.zip / HEAPUNT2.PAS < prev   
Pascal/Delphi Source File  |  1991-10-05  |  6KB  |  187 lines

  1. unit HeapUnt2;
  2.  
  3. interface
  4.  
  5. const  MAX=100;
  6.  
  7. type
  8.      DataType = string;
  9.      HeapType = record
  10.         data: array[1..MAX] of DataType;
  11.         next: integer;
  12.        end;
  13.  
  14.      HeapObj = object
  15.        Heap: HeapType;
  16.  
  17.        procedure Init;
  18.        procedure ReHeapUp;
  19.        procedure Insert(num: DataType);
  20.        procedure ReHeapDown;
  21.        function GetMin: DataType;
  22.        function Empty:  Boolean;
  23.        function TotalNum: integer;
  24.        function search(num: DataType): boolean;
  25.        end; { Object HeapObj }
  26.  
  27. implementation
  28.  
  29. {==========================================================================}
  30. { This procedure swaps the two inputs of DataType                          }
  31. {==========================================================================}
  32. procedure swap(var a,b: DataType);
  33.  
  34. var temp: DataType;
  35.  
  36. begin   { Procedure Swap }
  37.   temp:=a;
  38.   a:=b;
  39.   b:=temp;
  40. end;   { Procedure Swap }
  41.  
  42.  
  43. {==========================================================================}
  44. { This procedure initializes the Heap.                                     }
  45. {==========================================================================}
  46. procedure HeapObj.Init;
  47.  
  48. begin   { Procedure Init }
  49.   heap.next:=1;
  50. end;    { Procedure Init }
  51.  
  52.  
  53. {==========================================================================}
  54. { This procedure moves a priority up the heap until it is in the correct   }
  55. { position; ie, it is less than its children and greater than its parent   }
  56. {==========================================================================}
  57. procedure HeapObj.ReHeapUp;
  58.  
  59. var done: boolean;
  60.     i:    integer;
  61.  
  62. begin   { Procedure ReHeapUp }
  63.   done:=false;
  64.   i:=heap.next-1;
  65.  
  66.   while not done do
  67.    begin
  68.      if heap.data[i div 2] > heap.data[i] then      { if less than parent }
  69.        swap(heap.data[i div 2],heap.data[i])        { swap with its parent }
  70.      else
  71.        done:=true;
  72.      i:=i div 2;                                    { point to parent }
  73.      if i=1 then
  74.        done:=true;
  75.    end; { while }
  76. end;   { Procedure ReHeapUp }
  77.  
  78.  
  79. {==========================================================================}
  80. { This procedure inserts a new priority into the heap and reorders heap    }
  81. { to maintain its order.                                                   }
  82. {==========================================================================}
  83. procedure HeapObj.Insert(num: DataType);
  84.  
  85. begin   { Procedure Insert }
  86.   heap.data[heap.next]:=num;                        { put item into last pos. }
  87.   inc(heap.next);                                   { increment next ptr. }
  88.  
  89.   if heap.next > 2 then                             { re-heap up if needed. }
  90.     ReHeapUp;
  91. end;   { Procedure Insert }
  92.  
  93.  
  94. {==========================================================================}
  95. { This procedure moves a priority down the heap until it is in the correct }
  96. { position. (Only executed after a deletion from the heap.)                }
  97. {==========================================================================}
  98. procedure HeapObj.ReHeapDown;
  99.  
  100. var done: boolean;
  101.     i:    integer;
  102.  
  103. begin   { Procedure ReHeapDown }
  104.   i:=1;
  105.   done:=false;
  106.   while not done do
  107.    begin
  108.     if ( (i*2)+1 < heap.next) and (heap.data[i]<heap.data[i*2])
  109.       and (heap.data[i]<heap.data[(i*2)+1]) then
  110.         done:=true
  111.     else if ( (i*2)+1 >= heap.next) and (heap.data[i]<heap.data[i*2]) then
  112.       done:=true
  113.     else
  114.       begin
  115.        if ( (i*2)+1 < heap.next) and (heap.data[(i*2)+1]<heap.data[i*2]) then
  116.          begin
  117.            swap(heap.data[i],heap.data[(i*2)+1]);      { swap with right child }
  118.            i:=(i*2)+1;
  119.          end { if }
  120.        else
  121.          begin
  122.            swap(heap.data[i],heap.data[i*2]);          { swap with left child }
  123.            i:=i*2;
  124.          end; { if }
  125.  
  126.        if i>=heap.next div 2 then
  127.          done:= true;
  128.      end; { if }
  129.    end; { while }
  130. end;   { Procedure ReHeapDown }
  131.  
  132.  
  133. {==========================================================================}
  134. { This function returns the minimum priority and reorders the heap to      }
  135. { maintain its order.                                                      }
  136. {==========================================================================}
  137. function HeapObj.GetMin: DataType;
  138.  
  139. begin   { Function GetMin }
  140.   GetMin:=heap.data[1];                            { assign min priority }
  141.   dec(heap.next);                                  { decrement next ptr }
  142.  
  143.   heap.data[1]:=heap.data[heap.next];              { move last element to top }
  144.  
  145.   if heap.next > 2 then                            { re-heap down if needed }
  146.     ReHeapDown;
  147. end;   { Function GetMin }
  148.  
  149.  
  150. {==========================================================================}
  151. { This procedure tells if the Heap is empty.                               }
  152. {==========================================================================}
  153. function HeapObj.Empty: Boolean;
  154.  
  155. var temp: boolean;
  156.  
  157. begin   { Function Empty }
  158.   Empty:=heap.next=1;
  159. end;    { Function Empty }
  160.  
  161. {==========================================================================}
  162. { This procedure tells the number of heap elements                         }
  163. {==========================================================================}
  164. function HeapObj.TotalNum: integer;
  165.  
  166. begin { Function TotalNum }
  167.   TotalNum:=Heap.next-1;
  168. end;  { Function TotalNum }
  169.  
  170.  
  171. {==========================================================================}
  172. { This procedure tells if the item is in the heap.                         }
  173. {==========================================================================}
  174. function HeapObj.search(num: DataType): boolean;
  175.  
  176. var i:integer;
  177.  
  178. begin { Function Search }
  179.   search:=false;
  180.   for i:=1 to TotalNum do
  181.     if heap.data[i]=num then
  182.       search:=true;
  183. end;  { Function Search }
  184.  
  185.  
  186. begin
  187. end. { unit }